package com.hjq.demo.ui.adapter

import android.content.Context
import android.content.pm.PackageInfo
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import com.hjq.demo.R
import com.hjq.demo.app.AppAdapter
import timber.log.Timber

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject-Kotlin
 *    time   : 2019/09/22
 *    desc   : 状态数据列表
 */
class AppListAdapter constructor(context: Context) : AppAdapter<PackageInfo?>(context) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder()
    }

    inner class ViewHolder : AppViewHolder(R.layout.item) {

        private val tv_appname: TextView? by lazy { findViewById(R.id.tv_appname) }
        private val iv_appname: AppCompatImageView? by lazy { findViewById(R.id.iv_appname) }

        override fun onBindView(position: Int) {
            val info = getItem(position)?.applicationInfo
            val icon = getContext().packageManager.getApplicationIcon(info!!)
            val appName = getContext().packageManager.getApplicationLabel(info)
            tv_appname?.text = appName?.toString()
            Timber.i("name:${appName} pkg: ${info?.packageName}- ${info?.flags}")
            iv_appname?.setImageDrawable(icon!!);

        }
    }
}